package logbackLoggingExample;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReadXLS {

    final static Logger logger = LoggerFactory.getLogger(ReadXLS.class);

    public static void main(String[] args) {

        /*
         *  printing Logback Status
         *  If warnings or errors occur during the parsing of the configuration
         *  file, logback will automatically print status data on the console. 
         *  In the absence of warnings or errors, if you still wish to inspect 
         *  logback's internal status, then you can instruct logback to print 
         *  status data by invoking the print() of the StatusPrinter class.
         */
        
        // assume SLF4J is bound to logback in the current environment
        // for this you need logback-classic*.jar and logback-core*.jar
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        // print logback's internal status
        StatusPrinter.print(lc);
      
        logger.debug("hallo from logger");
        logger.info("an info log message");

        
        String fileName = "C:\\tmp\\testPOIRead.xls";
        //Read an Excel File and Store in a ArrayList
        //Notice:ArrayList is not thread-safe. You would have to use
        //Collections.synchronizedList(<your ArrayList>) instead for synch access.
        ArrayList dataHolder = readExcelFile(fileName);
        //Print the data read
        printCellDataToConsole(dataHolder);
    }

    public static ArrayList readExcelFile(String fileName) {
        /** --Define a ArrayList
        --Holds ArrayList Of Cells
         */
        ArrayList cellVectorHolder = new ArrayList();

        try {
            /** Creating Input Stream**/
            FileInputStream myInput = new FileInputStream(fileName);

            Workbook myWorkBook = WorkbookFactory.create(myInput);

            /** Get the first sheet from workbook**/
            Sheet mySheet = myWorkBook.getSheetAt(0);

            /** We now need something to iterate through the cells.**/
            Iterator rowIter = mySheet.rowIterator();

            while (rowIter.hasNext()) {
                Row myRow = (Row) rowIter.next();
                Iterator cellIter = myRow.cellIterator();
                ArrayList cellStoreVector = new ArrayList();
                while (cellIter.hasNext()) {
                    Cell myCell = (Cell) cellIter.next();
                    cellStoreVector.add(myCell);
                }
                cellVectorHolder.add(cellStoreVector);
            }
        } catch (Exception e) {
            logger.warn(" Exception " + e.fillInStackTrace().toString());
        }
        return cellVectorHolder;
    }

    private static void printCellDataToConsole(ArrayList dataHolder) {

        for (int i = 0; i < dataHolder.size(); i++) {
            ArrayList cellStoreVector = (ArrayList) dataHolder.get(i);

            for (int j = 0; j < cellStoreVector.size(); j++) {
                Cell myCell = (Cell) cellStoreVector.get(j);
                String stringCellValue = myCell.toString();
                System.out.print(stringCellValue + "\t");
            }
            System.out.println();
        }
    }
}
